<template>
  <div class="container" id="container">
    <div class="top">
      <header class="mask-paper">
        <a style="display: flex">
          <img class="totalLogo" style="width: 72px; height: 72px; margin-top: 20px" src="@/assets/logo1.png" />
          <span style="line-height: 100px; font-weight: bolder; font-size: 30px; margin-left: 20px; letter-spacing: 4px"
            >爱吃咸红薯的鱼</span
          >
        </a>
        <div class="tool-box"></div>
        <div class="input-box" id="sujContainer" v-show="$route.path === '/productInfo'">
          <input
            type="text"
            v-model="key"
            class="search-input"
            placeholder="搜索你想要的"
            @focus="focusInput"
            @keyup.enter="searchPage"
            ref="SearchInput"
          />
          <div class="input-button">
            <div class="search-icon" v-if="key === ''">
              <el-icon class="search-icon1" :size="16"><search /></el-icon>
            </div>
            <div class="close-icon" v-else @click="clearKeyword">
              <el-icon class="circle-close-icon" :size="16"><circle-close /></el-icon>
            </div>
          </div>
        </div>
        <div class="right"></div>
      </header>
    </div>
    <!--    main left and container-->
    <div class="main">
      <div class="side-bar">
        <ul class="channel-list">
          <li :class="$route.path === '/productInfo' ? 'active-channel' : ''">
            <a class="link-wrapper" @click="toProductInfo()">
              <div
                style="
                  width: 1.5em;
                  height: 1.5em;
                  margin-right: 8px;
                  display: flex;
                  align-items: center;
                  justify-content: center;
                "
              >
              <el-icon><House /></el-icon>
              </div>
              <span class="channel">推荐</span></a
            >
          </li>

          <li :class="$route.path === '/followtrend' ? 'active-channel' : ''">
            <a class="link-wrapper" @click="toTrend()">
              <div
                style="
                  width: 1.5em;
                  height: 1.5em;
                  margin-right: 8px;
                  display: flex;
                  align-items: center;
                  justify-content: center;
                "
              >
              <el-icon><Star /></el-icon>
              </div>
              <span class="channel"> 动态</span>
            </a>
          </li>
          <li :class="$route.path === '/notice' ? 'active-channel' : ''">
            <a class="link-wrapper" @click="toMessage()">
              <div
                style="
                  width: 1.5em;
                  height: 1.5em;
                  margin-right: 8px;
                  display: flex;
                  align-items: center;
                  justify-content: center;
                "
              >
                <el-badge v-if="noReadMessage" is-dot class="item"><i class="el-icon-bell"></i></el-badge>
                <el-icon><Bell /></el-icon>
              </div>
              <span class="channel">消息</span>
            </a>
          </li>
          <li :class="$route.path === '/publish' ? 'active-channel' : ''">
            <a class="link-wrapper" @click="toPush()">
              <div
                style="
                  width: 1.5em;
                  height: 1.5em;
                  margin-right: 8px;
                  display: flex;
                  align-items: center;
                  justify-content: center;
                "
              >
              <el-icon><CirclePlus /></el-icon>
              </div>

              <span class="channel"> 发布</span>
            </a>
          </li>
          <li style="background-color: #ff2e4d; border-radius: 999px" v-if="!loginStatus">
            <a class="link-wrapper" @click="toggleLogin()">
              <i class="el-icon-user" style="width: 1.5em; height: 1.5em; margin-right: 8px"></i>
              <span class="channel" style="color: white"> 登录</span>
            </a>
          </li>
          <li v-else :class="$route.path === '/user' ? 'active-channel' : ''">
            <a class="link-wrapper" @click="toUser">
              <el-image
                style="width: 1.5em; height: 1.5em; margin-right: 8px; border-radius: 100%"
                :src="userStore.userInfo.avatar"
              ></el-image>
              <!--              <i class="el-icon-user" style="width: 1em; height: 1em; margin-right: 8px"></i>-->
              <span class="channel"> 我的</span>
            </a>
          </li>
        </ul>
        <!--user information-->
        <div class="information-container">
          <div class="information-pad" v-show="moreStatus">
            <div class="container">
              <div>
                <div>
                  <div class="group-wrapper">
                    <div class="group-header">设置</div>
                    <div class="menu-item hover-effect">
                      <span>深色模式</span>
                      <div class="multistage-toggle component">
                        <button class="toggle-item active">
                          <div class="icon-wrapper"></div>
                        </button>
                      </div>
                    </div>
                    <div class="menu-item hover-effect">
                      <a @click="logout">
                        <i class="el-icon-turn-off"></i>
                        <span style="margin-left: 10px">退出登录</span>
                      </a>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="information-wrapper" @click="toggleMore()">
            <i class="el-icon-more" style="width: 1em; height: 1em; margin-right: 8px"></i>
            <span class="channel"> 更多</span>
          </div>
        </div>
      </div>
      <!-- main -->
      <div class="main-content with-side-bar">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';

import { getImageUrl } from '@/utils';
import { useUserStore } from '@/stores/user';
import eventBus from 'vue3-eventbus';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

// 定义响应式变量
const noReadMessage = ref(false);
const moreStatus = ref(false);
const showLogin = ref(false);
const key = ref('');
const loginStatus = ref(false);
let timer: number | undefined;

const checkLoginStatus = () => {
  if (userStore.userInfo) {
    loginStatus.value = true;
  } else {
    loginStatus.value = false;
  }
};
// const getNoReadMessageFlush = () => {
//   timer = setInterval(() => {
//     getNoReadMessage();
//   }, 2000);
// };

const handleMessage = (val: number) => {
  if (val > 0) {
    noReadMessage.value = true;
  } else {
    noReadMessage.value = false;
  }
};

// const getNoReadMessage = () => {
//   chatListApi.getNoReadCount().then((res) => {
//     if (res.result > 0) {
//       noReadMessage.value = true;
//     } else {
//       noReadMessage.value = false;
//     }
//   });
// };

const toProductInfo = () => {
  router.push('/productInfo');
};

const toTrend = () => {
  router.push('/followtrend');
};

const logout=()=>{
    userStore.logout();
}

const toMessage = () => {
  router.push('/notice');
};

const toPush = () => {
  router.push('/publish');
};

const toUser = () => {
  router.push('/user');
};

const toggleMore = () => {
  moreStatus.value = !moreStatus.value;
};

const toggleLogin = () => {
  if (loginStatus.value) {
    // 已登录，可以添加其他逻辑
  } else {
    showLogin.value = true;
  }
};

const clearKeyword = () => {
  key.value = '';
};

const focusInput = () => {
  // 这里需要定义一个 ref 来引用输入框
  // 例如：const SearchInput = ref<HTMLInputElement | null>(null);
  // 然后在模板中使用 ref="SearchInput"
  // 在这里使用 SearchInput.value?.focus();
};

const searchPage = () => {
  eventBus.emit('keyChanged', key.value);
};

const loginClose = (val: boolean) => {
  showLogin.value = false;
  loginStatus.value = val;
};



// 生命周期钩子
onMounted(() => {
  checkLoginStatus();
  
});

onUnmounted(() => {
  clearInterval(timer);
//   eventBus.off('noReadMessage', handleMessage);
  eventBus.off('keyChanged', searchPage);
});
</script>
<style lang="less" scoped>


.circle-close-icon{
  width: 1em;
  height: 1em;
  margin-right: 8px;
  margin-top: 5px;
}
.search-icon1 {
  width: 1em;
  height: 1em;
  margin-right: 8px;
  margin-top: 5px;


}
a {
  text-decoration: none;
  color: rgba(51, 51, 51, 0.8);
}

.container {
  max-width: 1728px;
  background-color: #fff;
  margin: 0 auto;

  .top {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100vw;
    height: 72px;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 10;
    align-items: center;
    background: #fff;

    header {
      position: relative;
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      max-width: 1728px;
      height: 72px;
      padding: 0 16px 0 24px;
      z-index: 10;

      .tool-box {
        width: 24px;
        height: 70px;
        position: absolute;
        left: 5px;
        top: 0;
      }

      .input-box {
        height: 40px;
        position: fixed;
        left: 50%;
        transform: translate(-50%);

        @media screen and (max-width: 695px) {
          display: none;
        }

        @media screen and (min-width: 960px) and (max-width: 1191px) {
          width: calc(-36px + 50vw);
        }

        @media screen and (min-width: 1192px) and (max-width: 1423px) {
          width: calc(-33.6px + 40vw);
        }

        @media screen and (min-width: 1424px) and (max-width: 1727px) {
          width: calc(-42.66667px + 33.33333vw);
        }

        @media screen and (min-width: 1728px) {
          width: 533.33333px;
        }

        .search-input {
          padding: 0 84px 0 16px;
          width: 100%;
          height: 100%;
          font-size: 16px;
          line-height: 120%;
          color: #333;
          caret-color: #ff2442;
          background: rgba(0, 0, 0, 0.03);
          border-radius: 999px;
        }

        .input-button {
          position: absolute;
          right: 0;
          top: 0;
          display: flex;
          align-items: center;
          justify-content: center;
          height: 100%;
          color: rgba(51, 51, 51, 0.8);
          cursor: pointer;
          .close-icon .search-icon {
            width: 40px;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: rgba(51, 51, 51, 0.8);
          }
        }
      }
    }
  }

  .main {
    display: flex;

    .side-bar {
      @media screen and (max-width: 695px) {
        display: none;
      }
      @media screen and (min-width: 696px) and (max-width: 959px) {
        display: none;
      }

      @media screen and (min-width: 960px) and (max-width: 1191px) {
        width: calc(-18px + 25vw);
        margin-left: 12px;
      }

      @media screen and (min-width: 1192px) and (max-width: 1423px) {
        width: calc(-16.8px + 20vw);
        margin-left: 12px;
      }

      @media screen and (min-width: 1424px) and (max-width: 1727px) {
        width: calc(-21.33333px + 16.66667vw);
        margin-left: 16px;
      }

      @media screen and (min-width: 1728px) {
        width: 266.66667px;
        margin-left: 16px;
      }

      height: calc(100vh - 72px);
      overflow-y: scroll;
      background-color: #fff;
      display: flex;
      flex-direction: column;
      flex-shrink: 0;
      padding-top: 16px;
      margin-top: 72px;
      position: fixed;
      overflow: visible;

      .channel-list {
        min-height: auto;
        -webkit-user-select: none;
        user-select: none;

        .active-channel {
          background-color: rgba(0, 0, 0, 0.03);
          border-radius: 999px;
          color: #333;
        }

        li:hover {
          background-color: rgba(0, 0, 0, 0.03);
          border-radius: 999px;
          color: #333;
        }

        li {
          padding-left: 16px;
          min-height: 48px;
          display: flex;
          align-items: center;
          cursor: pointer;
          margin-bottom: 8px;
          color: rgba(51, 51, 51, 0.6);

          .link-wrapper {
            display: flex;
            width: 100%;
            height: 48px;
            align-items: center;
          }

          .message-count {
            margin-left: 7rem;
            background-color: red;
            width: 20px;
            height: 20px;
            font-size: 14px;
            line-height: 20px;
            text-align: center;
            border-radius: 50%;
            color: #fff;
          }
        }

        .channel {
          font-size: 17px;
          font-weight: 600;
          margin-left: 12px;
          color: #333;
        }
      }

      .information-container {
        display: inline-block;
        width: 70%;
        color: #333;
        font-size: 16px;
        position: absolute;
        bottom: 0;

        .information-pad {
          z-index: 16;
          margin-bottom: 4px;
          width: 100%;

          .container {
            width: 100%;
            background: #fff;
            box-shadow:
              0 4px 32px 0 rgba(0, 0, 0, 0.08),
              0 1px 4px 0 rgba(0, 0, 0, 0.04);
            border-radius: 12px;

            .divider {
              margin: 0px 12px;
              list-style: none;
              height: 0;
              border: 1px solid rgba(0, 0, 0, 0.08);
              border-width: 1px 0 0;
            }

            .group-wrapper {
              padding: 4px;

              .group-header {
                display: flex;
                align-items: center;
                padding: 0 12px;
                font-weight: 400;
                height: 32px;
                color: rgba(51, 51, 51, 0.6);
                font-size: 12px;
              }

              .menu-item {
                height: 40px;
                color: rgba(51, 51, 51, 0.8);
                font-size: 16px;
                border-radius: 8px;
                display: flex;
                align-items: center;
                padding: 0 12px;
                font-weight: 400;
                cursor: pointer;
                .icon {
                  color: rgba(51, 51, 51, 0.3);
                  margin-left: auto;
                }

                .component {
                  margin-left: auto;
                }

                .multistage-toggle {
                  position: relative;
                  background: rgba(0, 0, 0, 0.03);
                  display: flex();
                  padding: 2px;
                  border-radius: 999px;
                  cursor: pointer;

                  .active {
                    background: #fff;
                    box-shadow:
                      0 2px 8px 0 rgba(0, 0, 0, 0.04),
                      0 1px 2px 0 rgba(0, 0, 0, 0.02);
                    color: #333;
                  }

                  .toggle-item {
                    border-radius: 999px;
                    background: transparent;
                    color: rgba(51, 51, 51, 0.6);

                    .icon-wrapper {
                      width: 24px;
                      height: 24px;
                      display: flex;
                      align-items: center;
                      justify-content: center;
                      cursor: pointer;
                    }
                  }
                }
              }
              .menu-item:hover {
                background-color: rgba(0, 0, 0, 0.03);
                border-radius: 999px;
                color: #333;
              }
            }
          }
        }

        .information-wrapper {
          -webkit-user-select: none;
          user-select: none;
          cursor: pointer;
          position: relative;
          margin-bottom: 20px;
          height: 48px;
          width: 100%;
          display: flex;
          font-weight: 600;
          align-items: center;
          border-radius: 999px;
        }
      }
    }

    .main-content {
      width: 100%;
    }

    .main-content {
      @media screen and (min-width: 960px) and (max-width: 1191px) {
        padding-left: calc(-6px + 25vw);
      }

      @media screen and (min-width: 1192px) and (max-width: 1423px) {
        padding-left: calc(-4.8px + 20vw);
      }

      @media screen and (min-width: 1424px) and (max-width: 1727px) {
        padding-left: calc(-5.33333px + 16.66667vw);
      }

      @media screen and (min-width: 1728px) {
        padding-left: 282.66667px;
      }
    }
  }
}
</style>
